package achievement.dao.Impl;

import achievement.dao.SzlPlatformDao;
import achievement.domain.SzlBook;
import achievement.domain.SzlPlatform;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SzlPlatformDaoImpl implements SzlPlatformDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(SzlPlatform szlPlatform) {
        String sql = "insert into szl_platform\n" +
                "(platform_name,platform_team,platform_time,platform_rank,platform_proof) \n" +
                "values(?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,szlPlatform.getPlatformName());
            psmt.setString(2, szlPlatform.getPlatformTeam());
            psmt.setObject(3,szlPlatform.getPlatformTime());
            psmt.setInt(4,szlPlatform.getPlatformRank());
            psmt.setString(5, szlPlatform.getPlatformProof());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(SzlPlatform szlPlatform) {
        String sql = "update szl_platform set";
        List<Object> param = new ArrayList<>();
        if (szlPlatform == null){
            return;
        }else{
            if(szlPlatform.getPlatformId() != null){
                sql += " platform_id = ?,";
                param.add(szlPlatform.getPlatformId());
            }
            if(szlPlatform.getPlatformName() != null){
                sql += " platform_name = ?,";
                param.add(szlPlatform.getPlatformName());
            }
            if(szlPlatform.getType() != null){
                sql += " type = ?,";
                param.add(szlPlatform.getType());
            }
            if(szlPlatform.getPlatformTeam() != null){
                sql += " platform_team = ?,";
                param.add(szlPlatform.getPlatformTeam());
            }
            if(szlPlatform.getPlatformTime() != null){
                sql += " platform_time = ?,";
                param.add(szlPlatform.getPlatformTime());
            }
            if(szlPlatform.getPlatformRank() != null){
                sql += " platform_rank = ?,";
                param.add(szlPlatform.getPlatformRank());
            }
            if(szlPlatform.getPlatformProof() != null){
                sql += " platform_proof = ?,";
                param.add(szlPlatform.getPlatformProof());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where platform_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,szlPlatform.getPlatformId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from szl_platform where platform_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public SzlPlatform selectById(Long id) {
        String sql = "select * from szl_platform where platform_id = ?";
        SzlPlatform szlPlatform = new SzlPlatform();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                szlPlatform.setPlatformId(rs.getLong(1));
                szlPlatform.setPlatformName(rs.getString(2));
                szlPlatform.setType(rs.getInt(3));
                szlPlatform.setPlatformTeam(rs.getString(4));
                szlPlatform.setPlatformTime(rs.getDate(5));
                szlPlatform.setPlatformRank(rs.getInt(6));
                szlPlatform.setPlatformProof(rs.getString(7));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return szlPlatform;
    }

    @Override
    public List<SzlPlatform> selectList(SzlPlatform szlPlatform) {
        String sql = "select * from szl_platform where 1=1";
        List<SzlPlatform> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (szlPlatform == null){
            sql = "select * from szl_platform";
        }else{
            if(szlPlatform.getPlatformId() != null){
                sql += " and platform_id = ?";
                param.add(szlPlatform.getPlatformId());
            }
            if(szlPlatform.getPlatformName() != null){
                sql += " and platform_name like \"%\"?\"%\"";
                param.add(szlPlatform.getPlatformName());
            }
            if(szlPlatform.getType() != null){
                sql += " and type = ?";
                param.add(szlPlatform.getType());
            }
            if(szlPlatform.getPlatformTeam() != null){
                sql += " and platform_team like \"%\"?\"%\"";
                param.add(szlPlatform.getPlatformTeam());
            }
            if(szlPlatform.getPlatformTime() != null){
                sql += " and platform_time = ?";
                param.add(szlPlatform.getPlatformTime());
            }
            if(szlPlatform.getPlatformRank() != null){
                sql += " and platform_rank = ?";
                param.add(szlPlatform.getPlatformRank());
            }
            if(szlPlatform.getPlatformProof() != null){
                sql += " and platform_proof like \"%\"?\"%\"";
                param.add(szlPlatform.getPlatformProof());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                SzlPlatform szlPlatform1 = new SzlPlatform();
                szlPlatform1.setPlatformId(rs.getLong(1));
                szlPlatform1.setPlatformName(rs.getString(2));
                szlPlatform1.setType(rs.getInt(3));
                szlPlatform1.setPlatformTeam(rs.getString(4));
                szlPlatform1.setPlatformTime(rs.getDate(5));
                szlPlatform1.setPlatformRank(rs.getInt(6));
                szlPlatform1.setPlatformProof(rs.getString(7));
                list.add(szlPlatform1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
